package com.itheima.sc.service.sms;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.convert.Convert;
import com.itheima.sc.api.sms.SmsCouponApi;
import com.itheima.sc.api.sms.SmsCouponProductCategoryRelationApi;
import com.itheima.sc.api.sms.SmsCouponProductRelationApi;
import com.itheima.sc.dto.CouponDto;
import com.itheima.sc.pojo.sms.Coupon;
import com.itheima.sc.pojo.sms.CouponProductCategoryRelation;
import com.itheima.sc.pojo.sms.CouponProductRelation;
import com.itheima.sc.vo.PageResult;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

@Service
public class SmsCouponService {

    @DubboReference
    private SmsCouponApi couponApi;
    @DubboReference
    private SmsCouponProductRelationApi productRelationApi;
    @DubboReference
    private SmsCouponProductCategoryRelationApi productCategoryRelationApi;

    //添加优惠券
    public void addCoupon(CouponDto couponDto) {
        //获取coupon 的对象并赋值
        Coupon coupon = new Coupon();
        BeanUtil.copyProperties(couponDto, coupon);
        //添加优惠券
        Long id = couponApi.addCoupon(coupon);
        //获取优惠券的id
        //优惠券和产品分类的关系表
        List<CouponProductCategoryRelation> productCategoryRelations = couponDto.getProductCategoryRelationList();
        for (CouponProductCategoryRelation productCategoryRelation : productCategoryRelations) {
            productCategoryRelation.setCouponId(id);
            productCategoryRelationApi.addProductCategoryRelation(productCategoryRelation);
        }
        //优惠券和产品的关系表
        List<CouponProductRelation> productRelations = couponDto.getProductRelationList();
        productRelationApi.addProductRelation(productRelations);
    }

    //删除优惠券
    public void deleteCoupon(Long id) {

        //1.先删除产品分类关系表的数据
        productCategoryRelationApi.deleteCategoryRelationByCouponId(id);
        //2.删除茶品关系表中的数据
        productRelationApi.deleteProductRelationByCouponId(id);
        //3.删除优惠券本身的信息
        couponApi.deleteCouponById(id);
    }

    //根据优惠券名称和类型分页获取优惠券列表
    public PageResult selectCouponList(Map<String, String> param) {
        String name = param.get("name");
        Integer page = Convert.toInt(param.get("pageNum"));
        Integer pageSize = Convert.toInt(param.get("pageSize"));
        Integer type = Convert.toInt(param.get("type"));
        return couponApi.selectCouponList(page, pageSize, type, name);
    }

    //修改优惠券
    public void updateCoupon(Long id, CouponDto couponDto) {
        //首先通过id查询出用户的关系信息
        List<CouponProductCategoryRelation> productCategoryRelations = productCategoryRelationApi.selectCategoryRelationByCouponId(id);
        List<CouponProductRelation> productRelations = productRelationApi.selectProductRelationByCouponId(id);
        //1.删除优惠券和产品分类的关系表
        if (CollUtil.isNotEmpty(productRelations)){
            productCategoryRelationApi.delectProductCategoryRelation(productCategoryRelations);
        }
        //2.删除优惠券和产品的关系表
       // productRelationApi.deleteProductRelation(productRelations);
        //获取coupon 的对象并赋值
        Coupon coupon = new Coupon();
        BeanUtil.copyProperties(couponDto, coupon);
        //修改优惠券信息
        couponApi.updateCoupon(coupon);
        //将新的关系表添加进去
        List<CouponProductCategoryRelation> categoryRelations = couponDto.getProductCategoryRelationList();
        for (CouponProductCategoryRelation categoryRelation : categoryRelations) {
            categoryRelation.setCouponId(id);
            productCategoryRelationApi.addProductCategoryRelation(categoryRelation);
        }
        List<CouponProductRelation> relations = couponDto.getProductRelationList();
        productRelationApi.addProductRelation(relations);
    }

    //查询优惠券详情
    public CouponDto selectCouponInfo(Long id) {
        //1.先查询产品分类关系表的数据
        List<CouponProductCategoryRelation> productCategorys = productCategoryRelationApi.selectCategoryRelationByCouponId(id);
        //2.查询茶品关系表中的数据
        List<CouponProductRelation> productRelations = productRelationApi.selectProductRelationByCouponId(id);
        //3.查询优惠券本身的信息
        Coupon coupon = couponApi.selectCouponById(id);
        //4.封装结果集
        CouponDto dto = new CouponDto();
        BeanUtil.copyProperties(coupon, dto);
        dto.setProductRelationList(productRelations);
        dto.setProductCategoryRelationList(productCategorys);
        return dto;
    }
}
